import java.lang.reflect.Field;
import java.util.Arrays;

/**
 * Created by xiebang
 * Description
 * User:小谢
 * Date:2021-12-27
 * Time:18:13
 */
public class TestDemo {
    public static void main27(String[] args) {

        String str = String.valueOf(97);//将整形转换为了字符串类型。"97"
        System.out.println(str);

    }
    public static void main26(String[] args) {
//        String str = "   helloxiebang   ";
//        System.out.print(str.trim());//去除左右空格
//        System.out.println("abcdef");
        String str = "abcdEF谢";
        System.out.println(str.toUpperCase());//新的字符串把所有的字符转换为大写
        System.out.println(str.toLowerCase());//新的字符串把所有字符转换为小写
        System.out.println(str.concat("xiexie"));//将指定字符串连接到此字符串的结尾
        System.out.println(str.length());//求字符串的长度
        //字符串不是以\0结尾。
        String s = "";
        System.out.println(s.isEmpty());
        System.out.println(str.isEmpty());

    }
    public static void main25(String[] args) {
        String str = "helloabcdef";
        String ret = str.substring(2);//lloabcdef
        System.out.println("从2下标开始截取"+ ret);
        String ret2 = str.substring(2,6);//lloa
        System.out.println("从2下标截取到6下标截止" + ret2);
    }
    public static void main24(String[] args) {
        String str = "name=xiebang&age=23";
        String[] strings = str.split("&");
        for (String s1:strings) {
            String[] strings1 = s1.split("=");
            for (String s2:strings1) {
                System.out.println(s2);
            }
        }
    }
    public static void main23(String[] args) {
        String str = "Java string-split#test";
        String[] str1 = str.split(" |-|#");
        for (String s:str1) {
            System.out.println(s);
        }
    }
    public static void main22(String[] args) {
        String str = "ab#abc#abcd";
        String[] strings = str.split("#",2);
        //ab
        //abc#abcd

        for (String s:strings) {
            System.out.println(s);
        }
        String[] strings1 = str.split("#",5);
        //ab
        //abc
        //abcd//以参数进行分割，参数不存在就不分割
        //第二个参数是分割的极限组数
        for (String s:strings1) {
            System.out.println(s);
        }
    }
    public static void main21(String[] args) {
        String str = "ab abc abcd";
        String[] strings = str.split(" ");
        for (String s:strings) {
            System.out.println(s);
        }
    }
    public static void main20(String[] args) {
        String str = "ababcabcd";
        System.out.println(str);
        String str2 = str.replace('a','g');//替换所有的'a'为’g‘
        String str3 = str.replace("ab","gg");//替换所有的“ab”为“gg”
        String str4 = str.replaceAll("ab","gg");//替换所有“ab”为“gg”
        String str1 = str.replaceFirst("a","g");//替换第一个’a‘为'g'
        System.out.println(str3);
        System.out.println(str4);
        System.out.println(str2);//替换所以的a
        System.out.println(str1);

    }
    public static void main19(String[] args) {
        String str = "abcdefgh";
//        System.out.println(str.indexOf("cdef"));//2.返回所在的位置.(而且智慧找第一次出现的)/若没有返回的是-1;
        System.out.println(str.indexOf("cdef",2));
    }
    public static void main18(String[] args) {
        String str = "abcdefgh";
        boolean flg = str.contains("cdef");//String实现了charSequence接口,所以可以使用CharSequence引用字符串对象(向上转型)
        System.out.println(flg);
    }
    public static void main17(String[] args) {
        String str = "abcdef";
        byte[] bytes = str.getBytes();
        System.out.println(Arrays.toString(bytes));
    }
    public static void main16(String[] args) {
        byte[] bytes = {97,98,99,100};
        String str = new String(bytes,1,2);
        System.out.println(str);
    }
    public static boolean isNumber(String str){
        if (str == null) return false;
        if (str.length() == 0) return  false; // 注意 null 与空串不一样
        char[] chars = str.toCharArray();
        for (char ch: chars
             ) {
            if (ch<'0' || ch >'9'){
                return false;
            }

        }
        return true;
    }
    public static void main15(String[] args) {
        String str = "1a23456";
        if (isNumber(str)){
            System.out.println("全是数字字符!");
        }else {
            System.out.println("不全是数字字符!");
        }
    }
    public static void main14(String[] args) {
        String str = "hello";
        char[] chars = str.toCharArray();
        System.out.println(Arrays.toString(chars));
    }
    public static void main13(String[] args) {
        char[] values = {'a','b','c','d','e'};
        String str2 = "hello";
        char ch = str2.charAt(1);
        System.out.println(ch);
    }
    public static void main12(String[] args) {
        char[] values = {'a','b','c','d','e'};
        String str = new String(values,1,2);
        System.out.println(str);
    }
    public static void main11(String[] args) throws NoSuchFieldException, IllegalAccessException {
        String str = "hello";
        Class c = String.class;
        Field field = c.getDeclaredField("value");
        field.setAccessible(true);
        char[] vals = (char[])  field.get(str);//字符串可以通过放射来修改

        vals[0] = 'G';
        System.out.println(str);
    }
    public static void func(String str){
        str = "bit";
    }
    public static void main10(String[] args) {
        String str = "gaobo";
        func(str);
        System.out.println(str);//没有改变str的引用
    }
    public static void main9(String[] args) {
        String str1 = "hello";
        String str2 = str1;
        str1 = "world";
        System.out.println(str1 == str2);//false;
    }
    public static void main8(String[] args) {
        String s3 = new String("1") + new String("1");
        String s4 = "11";
        s3.intern();
        System.out.println(s3 == s4);//false;
    }
    public static void main7(String[] args) {
        String s3 = new String("1") + new String("1");
        s3.intern();
        String s4 = "11";
        System.out.println(s3 == s4);
    }
    public static void main6(String[] args) {
        String str1 = "gaobo";
        String str2 = new String("gaobo");
        str2.intern();//手动把str2入池。有就不放进去反之亦然/
        System.out.println(str1 == str2);
    }
    public static void main5(String[] args) {
        String str1 = "gaobo";
        String str2 = new String("gao") + new String("bo");
        System.out.println(str1 == str2);
    }
    public static void main4(String[] args) {
        String str1 = "gaobo";
        String str2 = "gao" + new String("bo");
        System.out.println(str1 == str2);
    }
    public static void main3(String[] args) {
        String str1 = "gaobo";
        String str2 = "gao";
        String str3 =  str1 + "bo";
        System.out.println(str1 == str3);
    }
    public static void main2(String[] args) {
        String str1 = "gaobo";
        String str2 = "gao" + "bo";
        System.out.println(str1 == str2);
    }
    public static void main1(String[] args) {
        //方式一:
        String str1= "hahaha";

        //方式二:
        String str2 = new String("hahaha");

        //方式三：
        char[] chars = {'a','b','c'};
        String str3 = new String(chars);
        System.out.println(str3);
        System.out.println(str1.equals(str2));
        System.out.println(str1 == str2);
    }
}
